package rrrmai.viewbinder;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.jakewharton.rxbinding2.view.RxView;
import com.xgggh.rrrmai.R;
import com.xgggh.rrrmai.databinding.ItemPostBinding;

import java.io.Serializable;
import java.util.concurrent.TimeUnit;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import me.drakeet.multitype.ItemViewBinder;
import rrrmai.activity.CommentActivity;
import rrrmai.activity.DetailsActivity;
import rrrmai.activity.LabelActivity;
import rrrmai.listener.RecyclerViewItemClickListener;
import rrrmai.listener.RecyclerViewItemLongClickListener;

/**
 * Created by summer on 2018/2/28.
 */
public class PostViewBinder extends ItemViewBinder<Post, PostViewBinder.ViewHolder> {
    RecyclerViewItemLongClickListener onItemLongClick;
    RecyclerViewItemClickListener onItemClick;

    public PostViewBinder(RecyclerViewItemLongClickListener onItemLongClick, RecyclerViewItemClickListener onItemClick) {
        this.onItemLongClick = onItemLongClick;
        this.onItemClick = onItemClick;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        ItemPostBinding binding= DataBindingUtil.inflate(inflater,R.layout.item_post, parent, false);
        return new ViewHolder(binding.getRoot());
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull Post post) {
        ItemPostBinding binding=DataBindingUtil.getBinding(holder.itemView);
        binding.setPost(post);
        RxView.clicks(binding.name).throttleFirst(1, TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        startLabel(binding.getRoot().getContext(),binding.getPost());
                    }
                });

        RxView.clicks(binding.getRoot()).throttleFirst(1, TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        if (onItemClick!=null){
                            onItemClick.OnItemClick(binding.getRoot());
                        }
                        //startDetails(binding.getRoot().getContext(),binding.getPost());
                    }
                });

        RxView.longClicks(binding.getRoot()).throttleFirst(1, TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        //删除 记录
                        if (onItemLongClick!=null){
                            onItemLongClick.OnItemLongClick(binding.getRoot());
                        }
                    }
                });
        RxView.clicks(binding.refer).throttleFirst(1, TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        startComment(binding.getRoot().getContext(),binding.getPost());
                    }
                });
    }

    private void startLabel(Context context,Post post){
        Intent in = new Intent(context, LabelActivity.class);
        in.putExtra("data",post);
        context.startActivity(in);
    }

    private void startComment(Context context,Post post){
        Intent in = new Intent(context, CommentActivity.class);
        in.putExtra("data",post);
        context.startActivity(in);
    }

    private void startDetails(Context context,Post post){
        Intent in = new Intent(context, DetailsActivity.class);
        in.putExtra("id", post.getId());
        context.startActivity(in);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        ViewHolder(View itemView) {
            super(itemView);
        }
    }
}
